package com.person.mall.service.Impl;


import com.person.mall.dao.CategoryMapper;
import com.person.mall.pojo.Category;
import com.person.mall.service.ICategoryService;
import com.person.mall.vo.CategoryVo;
import com.person.mall.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


import static com.person.mall.consts.MallConst.PARENT_ID;

/**
 * @author 最酷的桃儿wjc
 * * @date 2022/5/15
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {
        //创建一级目录集合
        List<CategoryVo> categoryVoList = new ArrayList<>();
        //获取所有类目数据
        List<Category> categories = categoryMapper.selectByStatus();
        for (Category category : categories) {
            //获取所有parent_id为0的数据，也就是一级目录
            if (category.getParentId().equals(PARENT_ID)){
                CategoryVo categoryVo = new CategoryVo();
                BeanUtils.copyProperties(category,categoryVo);
                categoryVoList.add(categoryVo);
            }
        }
        //将一级目录集合作为参数，和所有数据作为参数进行二次匹配
        findSubCategorie(categoryVoList,categories);

        return ResponseVo.successGetData(categoryVoList);

    }

    /**
     * 这里就是递归查询了，如果递归写不出来可以先写几个重复的方法，然后直接套用
     * @param categoryVoList
     * @param categories
     */
    private void findSubCategorie(List<CategoryVo> categoryVoList,List<Category> categories){
        for (CategoryVo categoryVo : categoryVoList) {
            //创建二级子目录集合
            List<CategoryVo> subCategoryVoList = new ArrayList<>();
            for (Category category : categories) {
                //一级目录分类Id是二级子目录的parent_id，查出匹配数据放入子子目录集合当中
                if (categoryVo.getId().equals(category.getParentId())){
                    CategoryVo subCategoryVo = new CategoryVo();
                    BeanUtils.copyProperties(category,subCategoryVo);
                    subCategoryVoList.add(subCategoryVo);
                }
            }
            //这里用到了递归查询
            findSubCategorie(subCategoryVoList,categories);
            categoryVo.setSubCategories(subCategoryVoList);
        }
    }

    /**
     * 1.做了一个递归查询
     * 2.为了并且避免重复查询结果集 对方法进行了重载
     * @param id
     * @param resultSet
     */
    @Override
    public void findSubCategoryId(Integer id, Set<Integer> resultSet) {
        List<Category> categories = categoryMapper.selectByStatus();
        findSubCategoryId(id,resultSet,categories);
    }

    public void findSubCategoryId(Integer id, Set<Integer> resultSet,List<Category> categories) {
        for (Category category : categories) {
            if (category.getParentId().equals(id)){
                resultSet.add(category.getId());
                findSubCategoryId(category.getId(),resultSet,categories);
            }
        }
    }
}
